import 'package:flutter/material.dart';
import 'package:flutter_lui/flutter_lui.dart';
import 'package:flutter_lui/src/l_style.dart';

class LText extends StatelessWidget {
  /// 文本
  final String text;

  /// 文本大小
  final double? fontSize;

  /// 文本颜色
  final Color? color;

  /// 行高
  final double? lineHeight;

  /// 文本粗细
  final FontWeight? fontWeight;

  /// 文本对齐方式
  final TextAlign? align;

  /// 文本溢出方式
  final TextOverflow? overflow;

  /// 文本最大行数
  final int? maxLines;

  /// 文本删除线
  final bool deleteline;

  /// 文本下划线
  final bool underline;

  /// 文本阴影
  final List<Shadow>? shadow;

  /// 点击事件
  final Function()? onTap;

  /// 外边距
  /// - [1]=>all
  /// - [1,1]=>[上下，右左]
  /// - [1,1,1]=> [上，右左，下]
  /// - [1,1,1,1]=> [上，右，下，左]
  final List<double> margin;

  /// 左外边距
  final double? marginLeft;

  /// 右外边距
  final double? marginRight;

  /// 上外边距
  final double? marginTop;

  /// 下外边距
  final double? marginBottom;

  /// 上下外边距
  final double? marginVertical;

  /// 左右外边距
  final double? marginHorizontal;
  const LText(this.text,
      {this.fontSize,
      this.color,
      this.lineHeight,
      this.fontWeight,
      this.align,
      this.overflow,
      this.maxLines,
      this.deleteline = false,
      this.underline = false,
      this.shadow,
      this.onTap,
      this.margin = const [0],
      this.marginLeft,
      this.marginRight,
      this.marginTop,
      this.marginBottom,
      this.marginVertical,
      this.marginHorizontal,
      Key? key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    TextAlign textAlign = align ?? TextAlign.start;
    TextOverflow of = overflow ?? TextOverflow.visible;
    TextDecoration? td;
    if (underline) td = TextDecoration.underline;
    if (deleteline) td = TextDecoration.lineThrough;
    Widget view = Text(
      Characters(text).toList().join("\u{200B}"),
      textAlign: textAlign,
      overflow: of,
      maxLines: maxLines ?? 100,
      softWrap: true,
      textScaleFactor: 1.0,
      style: TextStyle(
          height: lineHeight ?? 1.4,
          decoration: td,
          color: color ?? LUI.instance.textColor,
          fontSize: fontSize ?? LStyle.fontSize,
          fontWeight: fontWeight,
          shadows: shadow),
    );

    if (onTap != null) {
      view = GestureDetector(
        onTap: onTap,
        child: view,
      );
    }
    var marginValue = findTRBL(margin);
    var mt = marginTop ?? marginVertical ?? marginValue[0] ?? 0;
    var mr = marginRight ?? marginHorizontal ?? marginValue[1] ?? 0;
    var mb = marginBottom ?? marginVertical ?? marginValue[2] ?? 0;
    var ml = marginLeft ?? marginHorizontal ?? marginValue[3] ?? 0;
    if (mt != 0 || mr != 0 || mb != 0 || ml != 0) {
      view = Padding(
          padding: EdgeInsets.only(top: mt, right: mr, bottom: mb, left: ml),
          child: view);
    }
    return view;
  }
}
